import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'LoadingButton.dart';

Future<void> showConfirmDialog(BuildContext context,
    {required Widget? content,
    required Future<void> Function() onConfirm,
    required String title,
    String confirmText = "确定"}) {
  var completer = Completer<void>();
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text(title),
          content: content,
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text(
                "取消",
                style: Theme.of(context).textTheme.bodyMedium,
              ),
            ),
            LoadingButton(
              onPressed: ()async{
                await onConfirm();
                completer.complete();
              },
              child: Text(confirmText),
            ),
          ],
        );
      },
    );
    return completer.future;
}

Future<void>showConfirmDeleteGoodsDialog(BuildContext context,
    {required String barcode,
    required String name,
    required Future<void> Function() onConfirm,}) {
  return showConfirmDialog(context,
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(name),
          Text(barcode),
        ],
      ),
      onConfirm: onConfirm,
      title: "确定要删除此商品？",
      confirmText: "删除");
}
